import java.util.ArrayList;
import java.util.Scanner;

import static java.lang.System.exit;

/**
 * @Author xiyuan
 * @Date 2021/12/24 13:10
 * @Version 1.0
 */
public class Main {
    //    这个用于存储所有账户的
    static ArrayList<Account> Accounts = new ArrayList<>();
    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("============欢迎访问记账系统=============");
            System.out.println("请输入你的操作");
            System.out.println("1.新建账户");
            System.out.println("2.添加账户金额");
            System.out.println("3.删除账户金额");
            System.out.println("4.修改账户金额");
            System.out.println("5.查询账户金额");
            System.out.println("6.删除所有账户金额");
            System.out.println("7.查询所有账户金额");
            System.out.println("8.退出系统");
            int i = scanner.nextInt();
            switch (i) {
                case 1:
                    newAccount();
                    break;
                case 2:
                    addAccount();
                    break;
                case 3:
                    delAccount();
                    break;
                case 4:
                    modAccount();
                    break;
                case 5:
                    queryAccount();
                    break;
                case 6:
                    delAllAccount();
                    break;
                case 7:
                    queryAllAccount();
                    break;
                case 8:
                    exit(0);
                default:
                    break;
            }
        }
    }

    //    用于新建一个账户
    public static void newAccount() {
        System.out.println("请输入你的账户名字");
        String username = scanner.next();
//        账户名字不能重复
        for (Account account : Accounts) {
            if (username.equals(account.getUsername())) {
                System.out.println("该用户名已存在，新建失败");
                return;
            }
        }
        Account account = new Account(username);
        Accounts.add(account);
        System.out.println("新建成功");
    }

    //    用于增加账户的金额
    public static void addAccount() {
        System.out.println("请输入你的账户名字");
        String username = scanner.next();
        System.out.println("请输入你要增加的金额");
        int money = scanner.nextInt();
        for (Account account : Accounts) {
            if (username.equals(account.getUsername())) {
                account.addMoney(money);
                System.out.println("添加金额成功");
                return;
            }
        }
        System.out.println("没有该账户信息");
    }

    //删除账户金额
    public static void delAccount() {
        System.out.println("请输入你的账户名字");
        String username = scanner.next();
        for (Account account : Accounts) {
            if (username.equals(account.getUsername())) {
                Accounts.remove(account);
                System.out.println("删除账户成功");
                return;
            }
        }
        System.out.println("没有这个账户");
    }

    //修改账户金额
    public static void modAccount() {
        System.out.println("请输入你的账户名字");
        String username = scanner.next();
        for (Account account : Accounts) {
            if (username.equals(account.getUsername())) {
                System.out.println("请输入要修改的金额");
                account.setMoney(scanner.nextInt());
                System.out.println("修改账户金额成功");
                return;
            }
        }
        System.out.println("没有这个账户");
    }

    public static void queryAccount() {
        System.out.println("请输入你的账户名字");
        String username = scanner.next();
        for (Account account : Accounts) {
            if (username.equals(account.getUsername())) {
                System.out.println(account);
                System.out.println("查询成功");
                return;
            }
        }
        System.out.println("没有这个账户");
    }

    //删除所有账户的金额
    public static void delAllAccount() {
        System.out.println("已经完全删除账户");
        Accounts.clear();
    }

    //    查询所有账户的金额
    public static void queryAllAccount() {
        if (Accounts.size() == 0) {
            System.out.println("没有任何账户");
            return;
        }
        for (Account account : Accounts) {
            System.out.println(account);
        }
    }

}
